package com.longjianghuasheng.logistics.business.freight

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.freight.adapter.TypeAdapter
import com.longjianghuasheng.logistics.business.home.ServiceType
import com.longjianghuasheng.logistics.business.home.TypeResponse
import com.longjianghuasheng.logistics.databinding.FragmentFreightBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.initGridDecoration
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.util.CacheUtil
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding

class FreightFragment :
    BaseFragment<FreightViewModel, FragmentFreightBinding>() {


    private val types by lazy {

        mutableListOf<TypeResponse>()

    }
    private val typeAdapter by lazy {
        TypeAdapter()
    }

    private val pagerAdapter by lazy {
        PagerAdapter()
    }
    private val fragments by lazy {
        mutableListOf<Fragment>()
    }

    override fun layoutId(): Int = R.layout.fragment_freight


    @SuppressLint("NotifyDataSetChanged")
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.run {
            space.addStatusBarTopPadding()


            rvType.initGridDecoration(
                spanCount = 4,
                space = 0F,
                headFootSpace = 0F,
                adapter = typeAdapter
            )









            typeAdapter.setOnItemClickListener { _, _, position ->

                typeAdapter.data.forEachIndexed { i, type ->


                    type.isChecked = i == position
                }
                typeAdapter.notifyDataSetChanged()

                viewPager.setCurrentItem(position, false)

            }

//            radioGroup.setOnCheckedChangeListener { radio, checkedId ->
//                when (checkedId) {
//                    R.id.rb_transportation -> viewPager.setCurrentItem(0, false)
//
//                    R.id.rb_site -> viewPager.setCurrentItem(1, false)
//
//                    R.id.rb_inspection -> viewPager.setCurrentItem(2, false)
//
//                    R.id.rb_merchants -> viewPager.setCurrentItem(3, false)
//
//                }
//
//            }
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            mViewModel.isFirst = false

            CacheUtil.service_type = it

            mDatabind.setData(it)
        }
        eventViewModel.toFreightEvent.observeInFragment(this) { id ->

            if (mViewModel.isFirst) {
                mViewModel.id = id
            } else {

                typeAdapter.data.forEach {
                    it.isChecked = it.id == id
                }
                typeAdapter.notifyDataSetChanged()

                mDatabind.viewPager.setCurrentItem(
                    typeAdapter.data.indexOfFirst { it.isChecked },
                    false
                )

            }
        }
    }


    private fun FragmentFreightBinding.setData(it: ServiceType) {
        types.clear()
        types.addAll(it.data?.toMutableList() ?: mutableListOf())
        val index = types.indexOfFirst { it.id == mViewModel.id }.let {
            if (it == -1) 0 else it

        }

        types[index].isChecked = true
        typeAdapter.setNewInstance(types.toMutableList())
        fragments.clear()


        LogUtils.json(types)

        types.forEachIndexed { i, type ->
            fragments.add(ShipmentServiceFragment(type.types, i + 1))
        }

        viewPager.run {
            isUserInputEnabled = false

            adapter = pagerAdapter
        }


        viewPager.setCurrentItem(index, false)

    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.service_type()
    }

    inner class PagerAdapter : FragmentStateAdapter(this@FreightFragment) {

        override fun getItemCount(): Int = types.size

        override fun createFragment(position: Int): Fragment = fragments[position]

    }
}